#include "server.h"
#include <iostream>
using namespace std;

Server::Server(QObject *parent):QTcpServer(parent)
{
    foreach(QString name,updateInfo.fileNameList){
        QFile upfile(name);
        if(upfile.open(QIODevice::ReadOnly)){
            fileArray.append(upfile.readAll());
        }else{
            cout <<"The" << qPrintable(name) << "open failed!";
        }
    }
}

Server::~Server() {
}

/*-------------------------------------------------------
 * 开始监听
 *------------------------------------------------------*/
bool Server::listenConnect()
{
    if(!this->listen(QHostAddress::Any,updateInfo.getPort()))
    {
        qDebug()<<"Update:listen on port 3327 failed!";
        this->close();
        return false;
    }
    qDebug()<<"Update:listen on port 3327,waiting connection...";
    return true;
}

/**-------------------------------------------------------
 * 当有客户端连接时运行此函数
 *------------------------------------------------------*/
void Server::incomingConnection(int socketId)
{
    qDebug()<<"Update:a client try to connect...";
    //连接客户端
    ConnectThread * thread = new ConnectThread(this,socketId,updateInfo.getVersion(),fileArray);
}
